/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.netgame;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.locks.Condition;

/**
 * @author Massimo Canonaco
 *
 */
public class RequestMainServerConnectionHandler extends Thread
{

	/** */
	private MainConnectionServer mainConnectionServerMonitor;
	
	/** */
	private WaitingPlayerInformationBean waitingBean;
	
	/** */
//	private 
	/** */
	private ServerCondition conditionMonitor;
	/** */
	public RequestMainServerConnectionHandler(final MainConnectionServer monitor, final WaitingPlayerInformationBean bean, ServerCondition object)
	{
		mainConnectionServerMonitor = monitor;
//		socket = connectionSocket;
		waitingBean = bean;
		conditionMonitor = object;
	}
//	@Override
	
	@Override
	public void run() 
	{
		try 
		{
			boolean in = false;
			String clientRequest = null, clientKey = null;
			

			BufferedReader iStream = new BufferedReader(new InputStreamReader(waitingBean.getConnectionSocket().getInputStream()));
			PrintWriter oStream = new PrintWriter(waitingBean.getConnectionSocket().getOutputStream(),true);

//			while(true)
//			{
//			System.out.println("a2");
			clientRequest = iStream.readLine();
//			System.out.println("letto prima coso brabo client");
			clientKey = iStream.readLine();
			
//			System.out.println("letto tutto brabo client");
			if(clientRequest != null )
			{
				System.out.println("il client richiede :"+clientRequest);
				
				if(clientRequest.equalsIgnoreCase(BoomerangProtocol.ADD_ME))
				{
					System.out.println("Aggiungo il giocatore in lista, mi serverebbero le sue info ora o dopo?!");
					mainConnectionServerMonitor.addNewEntry(waitingBean);
					oStream.println(Integer.toString(waitingBean.getKey()));
					in = true;
					while((in = mainConnectionServerMonitor.isIn(waitingBean)) && mainConnectionServerMonitor.noEnemy(waitingBean))
					{
						conditionMonitor.doWait();
					}
				}
				else if(clientRequest.equalsIgnoreCase(BoomerangProtocol.REMOVE_ME))
				{
					mainConnectionServerMonitor.removePlayer(clientKey);
					oStream.println(Integer.toString(waitingBean.getKey()));
//					cicle = false;
				}
			}
			// If we're out from "if" statements: it's an remove request, so IN is false, or an enemy has been found (IN true).
			if(in)
			{
				oStream.println(waitingBean.getArenaName());
				if(!mainConnectionServerMonitor.isValidMap(iStream.readLine(), waitingBean.getArenaName()))
				{
					oStream.println("nonvabenerisatamalefica");
					BoomerangProtocol.sendFile(mainConnectionServerMonitor.getMapFile(waitingBean.getArenaName()), waitingBean.getConnectionSocket());
				}
				else
				{
					oStream.println("ok");
				}
//					oStream.println(mainConnectionServerMonitor.getMapFile(waitingBean.getArenaName()));
				
				oStream.println(waitingBean.isBeServer());
				oStream.println(waitingBean.getConnectionSocket().getPort());
				WaitingPlayerInformationBean enemyBean = mainConnectionServerMonitor.getEnemy(waitingBean);
				oStream.println(enemyBean.getConnectionSocket().getPort());
				oStream.println(((InetSocketAddress)enemyBean.getConnectionSocket().getRemoteSocketAddress()).getHostString());
				oStream.close();
				iStream.close();
				waitingBean.getConnectionSocket().close();
			}
			
//			System.out.println("END DEL TH : "+getId());
//			}
		}catch (InterruptedException | IOException e) 
		{
			e.printStackTrace();
		}
		finally
		{
		}
	}
}
